// PosInfo.cpp : implementation file
//

#include "stdafx.h"
#include "VolDia.h"
#include "PosInfo.h"
//#include "Uti1090.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

#define NOT_PRES	"Not present"
#define PRES		"Present"

extern CVolDiaApp theApp;
/////////////////////////////////////////////////////////////////////////////
// CPosInfo dialog


CPosInfo::CPosInfo(CWnd* pParent /*=NULL*/)
	: CDialog(CPosInfo::IDD, pParent)
{
	//{{AFX_DATA_INIT(CPosInfo)
	m_LastHandling = _T("");
	m_TVInfo = _T("");
	m_CheckRotAll = FALSE;
	m_Zone = _T("");
	m_VirtCoord = _T("");
	m_CellType = _T("");
	m_PhysCoord = _T("");
	//}}AFX_DATA_INIT
	m_hIcon = AfxGetApp()->LoadIcon(IDI_VOLDIA);
}


void CPosInfo::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CPosInfo)
	DDX_Control(pDX, IDC_CELL_MANAG, m_bCellManag);
	DDX_Control(pDX, ID_DBOP, m_bDbop);
	DDX_Control(pDX, IDC_CHECKROTALL, m_CheckRotAllCtrl);
	DDX_Control(pDX, IDC_LSTOBJ, m_LstObj);
	DDX_Text(pDX, IDC_LAST_HANDLING, m_LastHandling);
	DDX_Text(pDX, IDC_TV_INFO, m_TVInfo);
	DDX_Check(pDX, IDC_CHECKROTALL, m_CheckRotAll);
	DDX_Text(pDX, IDC_EDITZONE, m_Zone);
	DDX_Text(pDX, IDC_VC, m_VirtCoord);
	DDX_Text(pDX, IDC_CELL_TYPE, m_CellType);
	DDX_Text(pDX, IDC_PC, m_PhysCoord);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CPosInfo, CDialog)
	//{{AFX_MSG_MAP(CPosInfo)
	ON_EN_SETFOCUS(IDC_EDITZONE, OnSetfocusEditzone)
	ON_EN_SETFOCUS(IDC_TV_INFO, OnSetfocusTvInfo)
	ON_EN_SETFOCUS(IDC_LAST_HANDLING, OnSetfocusLastHandling)
	ON_EN_SETFOCUS(IDC_CELL_TYPE, OnSetfocusCellType)
	ON_EN_SETFOCUS(IDC_VC, OnSetfocusVc)
	ON_EN_SETFOCUS(IDC_PC, OnSetfocusPc)
	ON_BN_CLICKED(ID_DBOP, OnDbop)
	ON_BN_CLICKED(IDC_CELL_MANAG, OnCellManag)
	ON_WM_CLOSE()
	ON_WM_SETFOCUS()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CPosInfo message handlers

RETCOND CPosInfo::SetInputCoord(int x, int y){
	RETSTAT		ret;
	RETCOND		retCnd;
	TAGVALEX	tagVal;
	t_cell		*cell;
	OBJNAME		objName;
	DWORD		year, month, day, hour, min, sec, msec;
	TCHAR		objtDescr[MAX_OBJ_DESCR];
	CString		firstRange;
	CString		secondRange;
	ZONENAME	zoneName;
	GENIDX		reactIdx;
	int			i, objInCell;
	int			mode;
	float		weight;
	float		x_off;
	float		y_off;
	bool		clusCasL;
	bool		falvlMount;
	bool		dimpMount;

	objInCell = 0;
	//Inizializzazione temporanea di x e y.//
	clusCasL = false;
	cell = new t_cell;
	if((x == -1) && (y == -1)){
		//Just refresh//
		x = this->vx;
		y = this->vy;
	}

	GetZoneIdx(ZONE_REACTOR, &reactIdx);
	GetDynCurrentMode(&mode);

	ret = GetCellVirtCoo(x, y, cell);
	if(ret == RT_NORMAL){
		this->vx = x;
		this->vy = y;
		if(theApp.m_pDlg->m_pCellsManag != NULL){
			theApp.m_pDlg->m_pCellsManag->m_vx = this->vx;
			theApp.m_pDlg->m_pCellsManag->m_vy = this->vy;
			theApp.m_pDlg->m_pCellsManag->UpdateVal();
		}

		GetZoneIdxVirtCoo(this->vx, this->vy, &this->zoneIdx);
		
		if(theApp.m_pDlg->m_pDB_op != NULL){
			theApp.m_pDlg->m_pDB_op->m_defVx = this->vx;
			theApp.m_pDlg->m_pDB_op->m_defVy = this->vy;
			theApp.m_pDlg->m_pDB_op->m_defZoneIdx = this->zoneIdx;
			theApp.m_pDlg->m_pDB_op->UpdateVal();
		}

		switch(cell->type){
		case CELL_FA:
			m_CellType.Format(_T("Fuel Assembly cell"));
			break;
		case CELL_HECA:
			m_CellType.Format(_T("Hermetic cask cell"));
			break;
		case CELL_FFDS_CASK:
			m_CellType.Format(_T("FFDS cask cell"));
			break;
		case CELL_COVER_PLUG:
			m_CellType.Format(_T("Plug seat /cluster viewing cell"));
			break;
		case CELL_COVER_PLUG_EX:
			m_CellType.Format(_T("Plug seat /cluster casing cell"));
			break;
		default:
			m_CellType.Format(_T("Unknown cell"));
			break;
		}
		
		m_VirtCoord.Format(_T("(%d,%d)"), cell->Virt_X, cell->Virt_Y);

		retCnd = TagRead(&theApp.m_pDlg->m_tiFALVMount, &tagVal);
		if(retCnd != R_NORMAL)
			falvlMount = false;
		else{
			if(GtBit((unsigned int)(tagVal.value.val_di), 15) == 1)
				falvlMount = true;
			else
				falvlMount = false;
		}

		retCnd = TagRead(&theApp.m_pDlg->m_tiDIMPMount, &tagVal);
		if(retCnd != R_NORMAL)
			dimpMount = false;
		else{
			if(GtBit((unsigned int)(tagVal.value.val_di), 15) == 1)
				dimpMount = true;
			else
				dimpMount = false;
		}

		if((dimpMount || falvlMount) && (mode != M_AUTOMATIC) && (mode != M_SEMIAUTO) && (this->zoneIdx == reactIdx)){
			GetFALVOffset(&x_off, &y_off);
			m_PhysCoord.Format(_T("(%.0f,%.0f)"), (float)(cell->Phys_X - x_off), (float)(cell->Phys_Y - y_off));
		}
		else
			m_PhysCoord.Format(_T("(%.0f,%.0f)"), cell->Phys_X, cell->Phys_Y);

		m_LstObj.DeleteAllItems();

		for(i = 0; i < N_MAX_NONAM_OBJ; i++){
			if(cell->objt_cell.obj[i] != 0){
				objInCell++;
				GetObjtDescription(cell->objt_cell.obj[i], objtDescr);
				m_LstObj.InsertItem(LVIF_TEXT, objInCell, objtDescr, NULL, NULL, NULL, NULL);
				if(i < N_MAX_NAMED_OBJ){
					GetObjName(cell->objt_cell.o_id[i], objName);
					m_LstObj.SetItemText(objInCell - 1, 1, objName);
					GetObjWeight(cell->objt_cell.o_id[i], &weight);
					_stprintf_s(objtDescr, _T("%.0f"), weight);
					m_LstObj.SetItemText(objInCell - 1, 2, objtDescr);
				}
				else{
					GetObjtWeight(cell->objt_cell.obj[i], &weight);
					m_LstObj.SetItemText(objInCell - 1, 1, _T("No name"));
					_stprintf_s(objtDescr, _T("%.0f"), weight);
					m_LstObj.SetItemText(objInCell - 1, 2, objtDescr);
				}
			}
		}
		
		GetZoneName(this->zoneIdx, zoneName);
		m_Zone.Format(_T("%s"), zoneName);
		if ((cell->Rot_Range.range_1[0] == -1.0) && (cell->Rot_Range.range_1[1] == -1.0))
			firstRange.Empty();
		else if(cell->Rot_Range.range_1[1] == -1.0) 
			firstRange.Format(_T("%3.0f"), cell->Rot_Range.range_1[0]);
		else
			firstRange.Format(_T("[%3.0f,%3.0f]"), cell->Rot_Range.range_1[0], cell->Rot_Range.range_1[1]);

		if ((cell->Rot_Range.range_2[0] == -1.0) && (cell->Rot_Range.range_2[1] == -1.0))
			secondRange.Empty();
		else if(cell->Rot_Range.range_2[1] == -1.0) 
			secondRange.Format(_T("%3.0f"), cell->Rot_Range.range_2[0]);
		else
			secondRange.Format(_T("[%3.0f,%3.0f]"), cell->Rot_Range.range_2[0], cell->Rot_Range.range_2[1]);

		if(firstRange.IsEmpty() && secondRange.IsEmpty())
			m_TVInfo.Format(_T("No angle"));
		else if(!firstRange.IsEmpty() && secondRange.IsEmpty())
			m_TVInfo = firstRange;
		else if(firstRange.IsEmpty() && !secondRange.IsEmpty())
			m_TVInfo = secondRange;
		else
			m_TVInfo = firstRange + _T(", ") + secondRange;

		ConvertFromApmsTimeEx(&cell->objt_cell.LastDate, &year, &month, &day, &hour, &min, &sec, &msec);
		if(cell->Rot_Flag == 0) 
			m_CheckRotAll = false;
		else
			m_CheckRotAll = true;	
		m_LastHandling.Format(_T(" %d/%d/%d  %d:%d:%d.%d"), day, month, year, hour, min, sec, msec);
	}
	else
		return (R_FAILED);
	
	this->m_CheckRotAllCtrl.EnableWindow(false);
	UpdateData(FALSE);
	
	cell = NULL;
	delete cell;
	return R_NORMAL;
}

void CPosInfo::OnSetfocusEditzone(){
	SetFocus();
}

void CPosInfo::OnSetfocusLastHandling(){
	SetFocus();
}

void CPosInfo::OnSetfocusTvInfo(){
	SetFocus();
}


BOOL CPosInfo::OnInitDialog() 
{
	CDialog::OnInitDialog();
	SetIcon(m_hIcon, TRUE);	

	m_LstObj.InsertColumn(1, _T("Type"), LVCFMT_LEFT, 125, 0);
	m_LstObj.InsertColumn(2, _T("Name"), LVCFMT_LEFT, 125, 0);
	m_LstObj.InsertColumn(3, _T("Weight"), LVCFMT_CENTER, 60, 0);

	CheckUser();


	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CPosInfo::OnSetfocusCellType() 
{
	SetFocus();
}

void CPosInfo::OnSetfocusVc() 
{
	SetFocus();	
}

void CPosInfo::OnSetfocusPc() 
{
	SetFocus();	
}

void CPosInfo::OnDbop(){
	RETSTAT		retSt;
	CRect	posRect;
	CRect	dbRect;
	int		loggedUser;
	TAGIDX	tagIdx;
	TAGVALEX	tagVal;
	
	GetTagIdx(TWN_START_MOV, &tagIdx);
	TagRead(&tagIdx, &tagVal);
	if(GtBit((short int)(tagVal.value.val_di), 15) == 1)
	{
		MessageBox(_T("FHM movment in progress, \nDB operations are forbidden."),  _T("Attention"), MB_OK | MB_ICONINFORMATION);
		return;
	}
	
	//Controllo l'utente//
	retSt = GetLoggedUserLevel(theApp.m_pDlg->m_opStation, &loggedUser);
	if(retSt != RT_NORMAL){
		MessageBox(_T("Unknown logged user."),  _T("Error"), MB_OK);
		return;
	}
	if((loggedUser == LEVEL2) || (loggedUser == LEVEL3) || (loggedUser == LEVEL4) || (loggedUser == LEVEL5)){
		MessageBox(_T("Insufficent privileges to modify DB."), _T("Warning"), MB_OK | MB_ICONWARNING);
		return;
	}


	if(theApp.m_pDlg->m_pDB_op != NULL){
		theApp.m_pDlg->m_pDB_op->DestroyWindow();
		delete theApp.m_pDlg->m_pDB_op;
		theApp.m_pDlg->m_pDB_op = NULL;
	}
	theApp.m_pDlg->m_pDB_op = new CDB_op;
	theApp.m_pDlg->m_pDB_op->Create(IDD_DB_OP, NULL);
	theApp.m_pDlg->m_pDB_op->m_defVx = this->vx;
	theApp.m_pDlg->m_pDB_op->m_defVy = this->vy;
	theApp.m_pDlg->m_pDB_op->m_defZoneIdx = this->zoneIdx;
	theApp.m_pDlg->m_pDB_op->ShowWindow(SW_SHOW);
	this->GetWindowRect(&posRect);
	theApp.m_pDlg->m_pDB_op->GetWindowRect(&dbRect);
	theApp.m_pDlg->m_pDB_op->MoveWindow(posRect.right, posRect.top, dbRect.Width(), dbRect.Height(), TRUE);
		
}

void CPosInfo::OnCellManag(){
	RETSTAT		retSt;
	CRect		posRect;
	CRect		cellRect;
	int			loggedUser;
	
	//Controllo l'utente//
	retSt = GetLoggedUserLevel(theApp.m_pDlg->m_opStation, &loggedUser);
	if(retSt != RT_NORMAL){
		MessageBox(_T("Unknown logged user."),  _T("Error"), MB_OK);
		return;
	}
	if((loggedUser == LEVEL4) || (loggedUser == LEVEL5)){
		MessageBox(_T("Insufficent privileges to modify cells parameters."), _T("Warning"), MB_OK | MB_ICONWARNING);
		return;
	}

	if(theApp.m_pDlg->m_pCellsManag == NULL){
		theApp.m_pDlg->m_pCellsManag = new CellsManag(this->vx, this->vy, this);
		if(theApp.m_pDlg->m_pCellsManag != NULL){
			theApp.m_pDlg->m_pCellsManag->Create(IDD_CELLS_MANAG, NULL);
			theApp.m_pDlg->m_pCellsManag->ShowWindow(SW_SHOW);
			this->GetWindowRect(&posRect);
			theApp.m_pDlg->m_pCellsManag->GetWindowRect(&cellRect);
			theApp.m_pDlg->m_pCellsManag->MoveWindow(posRect.right, posRect.top, cellRect.Width(), cellRect.Height(), TRUE);

		}
		else
			MessageBox(_T("Error inizializing window pointer."),  _T("Error"), MB_OK|MB_ICONERROR);
	}
	else{
		theApp.m_pDlg->m_pCellsManag->m_vx = this->vx;
		theApp.m_pDlg->m_pCellsManag->m_vy = this->vy;
		theApp.m_pDlg->m_pCellsManag->UpdateVal();
		theApp.m_pDlg->m_pCellsManag->ShowWindow(SW_SHOW);	
	}
}

void CPosInfo::OnCancel(){
	if(theApp.m_pDlg->m_pCellsManag != NULL){
		//theApp.m_pDlg->m_pCellsManag->ShowWindow(SW_HIDE);
		theApp.m_pDlg->m_pCellsManag->DestroyWindow();
		delete theApp.m_pDlg->m_pCellsManag;
		theApp.m_pDlg->m_pCellsManag = NULL;
	}
	if(theApp.m_pDlg->m_pDB_op != NULL){
		theApp.m_pDlg->m_pDB_op->DestroyWindow();
		delete theApp.m_pDlg->m_pDB_op;
		theApp.m_pDlg->m_pDB_op = NULL;
	}
	
	CDialog::OnCancel();
}

void CPosInfo::OnClose(){
	if(theApp.m_pDlg->m_pCellsManag != NULL){
		//theApp.m_pDlg->m_pCellsManag->ShowWindow(SW_HIDE);
		theApp.m_pDlg->m_pCellsManag->DestroyWindow();
		delete theApp.m_pDlg->m_pCellsManag;
		theApp.m_pDlg->m_pCellsManag = NULL;
	}
	if(theApp.m_pDlg->m_pDB_op != NULL){
		theApp.m_pDlg->m_pDB_op->DestroyWindow();
		delete theApp.m_pDlg->m_pDB_op;
		theApp.m_pDlg->m_pDB_op = NULL;
	}
	
	CDialog::OnClose();
}

BOOL CPosInfo::SetControls()
{
	RETSTAT		retSt;
	
	//Controllo l'utente//
	retSt = GetLoggedUserLevel(theApp.m_pDlg->m_opStation, &this->m_loggedUser);
	if(retSt != RT_NORMAL){
		MessageBox(_T("Unknown logged user."),  _T("Error"), MB_OK);
		return false;
	}
	switch(this->m_loggedUser){
	case SYSMGR:
	case LEVEL1:
		this->m_bCellManag.EnableWindow(true);
		this->m_bDbop.EnableWindow(true);
		break;
	case LEVEL2:
		this->m_bCellManag.EnableWindow(true);
		this->m_bDbop.EnableWindow(false);
		break;
	case LEVEL3:
		this->m_bCellManag.EnableWindow(true);
		this->m_bDbop.EnableWindow(false);
		break;
	case LEVEL4:
		this->m_bCellManag.EnableWindow(false);
		this->m_bDbop.EnableWindow(false);
		break;
	case LEVEL5:
		this->m_bCellManag.EnableWindow(false);
		this->m_bDbop.EnableWindow(false);
		break;
	}
	return true;
}

bool CPosInfo::CheckUser()
{
	RETSTAT		retSt;
	int			currentUser;
	
	currentUser = this->m_loggedUser;
	//Controllo l'utente//
	retSt = GetLoggedUserLevel(theApp.m_pDlg->m_opStation, &this->m_loggedUser);
	if(retSt != RT_NORMAL){
		::MessageBox(this->m_hWnd, _T("Unknown logged user."),  _T("Error"), MB_OK);
		return false;
	}
	//if(m_loggedUser != currentUser){
		this->SetControls();
		//return true;
	//}
	return false;
}

void CPosInfo::OnSetFocus(CWnd* pOldWnd) 
{
	CDialog::OnSetFocus(pOldWnd);
	
	CheckUser();
	
}
